﻿using NewfarTormentTool.control;
using NewfarTormentTool.core;
using NewfarTormentTool.data;
using NewfarTormentTool.sub;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace NewfarTormentTool
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            Combo_User.DisplayMember = "Value";
            Combo_User.ValueMember = "Key";
            RoleDataStorage.Instance.Init();
            RefreshMain();
            RefreshCombo();
            RefreshSaveButton();
            RoleDataStorage.Instance.Main = this;
        }

        /// <summary>
        /// 关于面板
        /// </summary>
        private About AboutForm = null;

        /// <summary>
        /// 刷新主界面
        /// </summary>
        public void RefreshMain()
        {
            if (Panel_All.Controls.Count == 0)
            {
                // 加载所有角色
                foreach (UserRole role in RoleDataStorage.Instance.RolesInRow())
                {
                    RoleItemControl control = new RoleItemControl();
                    control.SetUpRole(role.Config, role.Active, false);
                    Panel_All.Controls.Add(control);
                }
            }
            else
            {
                int index = 0;
                // 加载所有角色
                foreach (UserRole role in RoleDataStorage.Instance.RolesInRow())
                {
                    RoleItemControl control = (RoleItemControl)Panel_All.Controls[index];
                    control.SetUpRole(role.Config, role.Active, false);
                    index++;
                }
            }
        }

        /// <summary>
        /// 刷新用户选择列表
        /// </summary>
        /// <param name="user">选中的用户名</param>
        public void RefreshCombo()
        {
            string user = RoleDataStorage.Instance.CurUser;
            Combo_User.Items.Clear();
            Combo_User.Items.Add(new KeyValuePair<string, string>("$", "自定义"));
            foreach (UserRecord record in RoleDataStorage.Instance.Records.Values)
            {
                Combo_User.Items.Add(new KeyValuePair<string, string>(record.user, record.user));
            }
            string target = user == null ? "$" : user;
            Combo_User.SelectedIndex = 0;
            int index = 0;
            foreach (KeyValuePair<string, string> pair in Combo_User.Items)
            {
                if (pair.Key.Equals(target))
                {
                    Combo_User.SelectedIndex = index;
                    break;
                }
                index++;
            }
        }

        /// <summary>
        /// 刷新存储按钮状态
        /// </summary>
        public void RefreshSaveButton()
        {
            Btn_Save.Enabled = RoleDataStorage.Instance.Changed();
        }

        /// <summary>
        /// 抽取
        /// </summary>
        private void Btn_Rand_Click(object sender, System.EventArgs e)
        {
            int num;
            try
            {
                num = int.Parse(TB_RandNum.Text);
                if (num <= 0) 
                {
                    throw new Exception("Wrong Number!");
                }
            }
            catch
            {
                MessageBox.Show("请输入正确的抽取数量!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            List<UserRole> result = RoleDataStorage.Instance.RandomRoles(num);
            if (result.Count == 0)
            {
                MessageBox.Show("可选角色不足!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            Panel_Result.Controls.Clear();
            foreach (UserRole role in result)
            {
                RoleItemControl control = new RoleItemControl();
                control.SetUpRole(role.Config, role.Active, true);
                Panel_Result.Controls.Add(control);
            }
        }

        /// <summary>
        /// 点击存储
        /// </summary>
        private void Btn_Save_Click(object sender, EventArgs e)
        {
            if (RoleDataStorage.Instance.CurUser != null)
            {
                UserRecord record = new UserRecord();
                record.user = RoleDataStorage.Instance.CurUser;
                record.actives = RoleDataStorage.Instance.UserRoles.Values.Where(r => r.Active).Select(r => r.Config.id).ToList();
                RoleDataStorage.Instance.AddOrUpdateRecord(RoleDataStorage.Instance.CurUser, record);
                RefreshSaveButton();
            }
            else
            {
                SaveUser newForm = new SaveUser();
                newForm.SetMain(this);
                newForm.ShowDialog();
            }
        }

        /// <summary>
        /// 用户变更
        /// </summary>
        private void Combo_User_SelectedIndexChanged(object sender, EventArgs e)
        {
            KeyValuePair<string, string> item = (KeyValuePair<string, string>)Combo_User.SelectedItem;
            string user = item.Key;
            if ("$".Equals(user) )
            {
                user = null;
            }
            RoleDataStorage.Instance.CurUser = user;
            RoleDataStorage.Instance.LoadUserRoles(user);
            RefreshMain();
            RefreshSaveButton();
        }

        /// <summary>
        /// 导入
        /// </summary>
        private void Btn_Import_Click(object sender, EventArgs e)
        {
            ImportUser newForm = new ImportUser();
            newForm.SetMain(this);
            newForm.ShowDialog();
        }

        /// <summary>
        /// 打开关于面板
        /// </summary>
        private void MenuItem_About_Click(object sender, EventArgs e)
        {
            if (AboutForm == null)
            {
                AboutForm = new About();
                AboutForm.Owner = this;
            }
            AboutForm.ShowDialog();
        }
    }
}
